<template>
  <div style="width: 100%;height: 100%;">
    <el-upload
      ref="uploadRef"
      action="#"
      list-type="picture"
      :limit="1"
      :auto-upload="false"
      :on-change="fileChange"
    >
      <i slot="default" class="el-icon-plus"></i>
      <div slot="file" slot-scope="{file}">
        <img
          class="el-upload-list__item-thumbnail"
          :src="file.url ? file.url : fileurl" alt=""
        >
        <span class="el-upload-list__item-actions">
        <span
          class="el-upload-list__item-preview"
          @click="handlePictureCardPreview(file)"
        >
          <i class="el-icon-zoom-in"></i>
        </span>
        <span
          v-if="!disabled"
          class="el-upload-list__item-delete"
          @click="handleDownload(file)"
        >
          <i class="el-icon-download"></i>
        </span>
        <span
          v-if="!disabled"
          class="el-upload-list__item-delete"
          @click="handleRemove(file)"
        >
          <i class="el-icon-delete"></i>
        </span>
      </span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      fileurl: ''
    }
  },
  props: {
    value:{}
  },
  watch: {
    value(file){
      this.fileurl = file.url
    }
  },
  methods: {
    handleRemove(file) {
      // 清除上传文件列表
      this.$refs.uploadRef.clearFiles();
      this.dialogImageUrl = '';
      this.$emit('input', null);
    },
    fileChange(file, fileList){
      this.$emit('input',file.raw)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleDownload(file) {
      const link = document.createElement('a');
      link.href = file.url;
      link.download = file.name || 'download';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
  }
}
</script>


<style scoped lang="scss">

</style>
